<template>
  <div>
    <!-- 搜索的布局 -->
    <el-form class="el-form" v-model="form">
      <el-form-item class="el-form-item" label="讲师名称">
        <el-input
          class="el-input"
          v-model="form.name"
          placeholder="请输入讲师关键字"
        ></el-input>
      </el-form-item>
      <el-form-item class="el-form-item" label="手机号">
        <el-input
          class="el-input"
          v-model="form.phone"
          placeholder="根据手机号搜索"
        ></el-input>
      </el-form-item>
      <div class="w1">
        <el-select v-model="select" class="xl" placeholder="请选择">
          <el-option
            v-for="item in tit"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button icon="el-icon-search" class="el-button" @click="sou"
          >搜索</el-button
        >
        <el-button icon="el-icon-loading" class="el-button" @click="chong"
          >重置</el-button
        >
      </div>
    </el-form>
    <!-- 渲染表格数据 -->
    <el-button type="primary" class="shan" @click="xuan">删除选中</el-button>
    <el-table
      :data="list"
      ref="multipleTable"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="sel"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        label="讲师名称"
        width="120"
        prop="real_name"
      ></el-table-column>
      <el-table-column fixed prop="data" label="头像" width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px; border-radius: 50%"
            :src="scope.row.avatar"
            :preview-src-list="imglist"
            @click="imglist.push(scope.row.avatar)"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="150" prop="mobile">
      </el-table-column>
      <el-table-column label="课程" width="150" prop="count"> </el-table-column>
      <el-table-column label="序号" width="150" prop="sort"> </el-table-column>
      <el-table-column label="审核状态" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.approval_status == 1">审核中</span>
          <span v-if="scope.row.approval_status == 2">审核通过</span>
          <span v-if="scope.row.approval_status == 3">审核不通过</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="190">
        <template slot-scope="scope">{{
          scope.row.created_at | filtime
        }}</template>
      </el-table-column>
      <el-table-column label="操作" width="190">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="del(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="statusChage"
      @current-change="currentChange"
      :page-sizes="[2, 3, 5]"
      :current-page="page"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 删除的弹框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>{{ this.tan }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { jiang, delsw } from "../../api/api";
export default {
  components: {},
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        phone: "",
      },
      select: "",
      tit: [
        {
          label: "审核中",
          value: 1,
        },
        {
          label: "审核通过",
          value: 2,
        },
        {
          label: "审核不通过",
          value: 3,
        },
      ],
      list: [],
      imglist: [],
      iid: "",
      page: 1,
      limit: 5,
      total: 0,
      tan: [],
    };
  },
  // 时间的过滤
  filters: {
    filtime(val) {
      return new Date(val * 1000).toLocaleString();
    },
  },
  mounted() {
    // 请求讲师数据
    this.feng();
  },
  methods: {
    // 把所有要选中删除的id存成一个字符串
    sel(val) {
      let res = val.map((item) => {
        return item.id;
      });
      this.removelds = res.join(",");
    },
    // 删除所选
    xuan() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list = this.list.filter((item) => {
            if (this.removelds.indexOf(item.id) == -1) {
              return item;
            }
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 单条删除
    del(id) {
      this.iid = id;
      delsw(this.iid).then((res) => {
        this.tan = res.data.msg;
      });
      this.dialogVisible = true;
    },
    // 搜索
    sou() {
      this.feng();
    },
    // 封装函数
    feng() {
      jiang({
        real_name: this.form.name,
        mobile: this.form.phone,
        approval_status: this.select,
        page: this.page,
        limit: this.limit,
      }).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
      });
    },
    // 重置
    chong() {
      this.form.name = "";
      this.form.phone = "";
      this.select = "";
      this.feng();
    },
    // 每页几条数据
    statusChage(limit) {
      this.limit = limit;
      this.feng();
    },
    // 上一页下一页
    currentChange(page) {
      this.page = page;
      this.feng();
    },
  },
  computed: {},
  watch: {},
};
</script>
<style scoped>
.el-form {
  width: 1200px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-input {
  width: 300px;
}
.w1 {
  margin-top: -20px;
}
.xl {
  width: 120px;
}
.el-form-item {
  width: 380px;
}
.el-button {
  margin-left: 10px;
}
.shan {
  float: left;
}
</style>
